package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.common.SnowFlake;
import com.example.demo.dto.Gkhomework.GkhomeworkQueryRequest;
import com.example.demo.dto.Gkhomeworkorder.GkhomeworkorderQueryRequest;
import com.example.demo.entity.Gkhomework;
import com.example.demo.entity.Gkhomeworkorder;
import com.example.demo.enums.PayStateEnum;
import com.example.demo.exception.CustomException;
import com.example.demo.service.GkhomeworkorderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/gkhomeworkorder")
public class GkhomeworkorderController {
    @Resource
    private GkhomeworkorderService gkhomeworkorderService;


    @PostMapping("/list/page")
    public Result listGkhomeworkorderByPage(@RequestBody GkhomeworkorderQueryRequest gkhomeworkorderQueryRequest) {
        long current = gkhomeworkorderQueryRequest.getCurrent();
        long size = gkhomeworkorderQueryRequest.getPageSize();
        if (current <= 0) {
            current = 1;
        }
        if (size <= 0) {
            size = 10;
        }

        // 查询数据库
        Page<Gkhomeworkorder> orderPage = gkhomeworkorderService.page(new Page<>(current, size),
                gkhomeworkorderService.getQueryWrapper(gkhomeworkorderQueryRequest));

        return Result.success(orderPage);
    }

    @GetMapping("/get")
    public Result getGkhomeworkorder( GkhomeworkorderQueryRequest gkhomeworkorderQueryRequest) {
        long current = gkhomeworkorderQueryRequest.getCurrent();
        long size = gkhomeworkorderQueryRequest.getPageSize();
        Page<Gkhomeworkorder> gkhomeworkPage = gkhomeworkorderService.page(new Page<>(current, size),
                gkhomeworkorderService.getQueryWrapper(gkhomeworkorderQueryRequest));
        return Result.success(gkhomeworkPage);
    }
    //添加
    @PostMapping("/add")
    public Result addGkhomeworkorder(@RequestBody Gkhomeworkorder gkhomeworkorder) {
        // 获取当前日期
        Date currentDate = new Date();
        gkhomeworkorder.setCreatetime(currentDate);

        SnowFlake idWorker = new SnowFlake(0, 0);
        String number = String.valueOf(idWorker.nextId());
        // 获取订单编号
         gkhomeworkorder.setNumber("GK"+number);

        // 保存订单
        boolean result = gkhomeworkorderService.save(gkhomeworkorder);
        return result ? Result.success("添加成功") : Result.error("添加失败");
    }

    @DeleteMapping("/delete")
    public Result deleteGkhomeworkorder(Integer id ) {
        if (id == null || id <= 0) {
            throw new CustomException("参数异常");
        }
        boolean result = gkhomeworkorderService.removeById(id);
        return result ? Result.success("删除成功") : Result.error("删除失败");
    }

    @PostMapping("/edit")
    public Result editGkhomeworkorder(@RequestBody Gkhomeworkorder gkhomeworkorder) {
        // 获取当前日期
        Date currentDate = new Date();
        gkhomeworkorder.setCreatetime(currentDate);
        boolean result = gkhomeworkorderService.updateById(gkhomeworkorder);
        return result ? Result.success("修改成功") : Result.error("修改失败");
    }

    //退款功能
    @PostMapping("/refund")
    public Result refundGkhomeworkorder(@RequestBody Gkhomeworkorder gkhomeworkorder) {

        if (gkhomeworkorder.getId() == null || gkhomeworkorder.getId() <= 0) {
            throw new CustomException("参数异常");
        }
        Gkhomeworkorder oldorder = gkhomeworkorderService.getById(gkhomeworkorder.getId());
        if (oldorder == null) {
            throw new CustomException("订单不存在");
        }
        if (oldorder.getState().equals(PayStateEnum.REFUND.getValue())) {
            throw new CustomException("订单已退款");
        }
        if (oldorder.getState().equals(PayStateEnum.WAIT_PAY.getValue())) {
            throw new CustomException("订单待支付");
        }
        if (oldorder.getState().equals(PayStateEnum.UNPAID.getValue())) {
            throw new CustomException("订单未支付");
        }

        if (oldorder.getState().equals(PayStateEnum.PAID.getValue())) {
            // 获取当前日期
            Date currentDate = new Date();
            oldorder.setCreatetime(currentDate);
            oldorder.setState(PayStateEnum.REFUND.getValue());
        }

        boolean result = gkhomeworkorderService.updateById(oldorder);
        return result ? Result.success("退款成功") : Result.error("退款失败");
    }










}
